package com.ruoyi.project.men.service.impl;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.men.dao.MenNeedInfoDao;
import com.ruoyi.project.men.dao.MenNeedTypeDao;
import com.ruoyi.project.men.domain.MenNeedInfoDO;
import com.ruoyi.project.men.service.MenNeedInfoService;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.MemberUserDO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MenNeedInfoServiceImpl implements MenNeedInfoService {
	@Autowired
	private MenNeedInfoDao menNeedInfoDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private MenNeedTypeDao menNeedTypeDao;
	
	@Override
	public MenNeedInfoDO get(String id){
		return menNeedInfoDao.get(id);
	}
	
	@Override
	public List<MenNeedInfoDO> list(Map<String, Object> map){
		return menNeedInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return menNeedInfoDao.count(map);
	}
	
	@Override
	public int save(MenNeedInfoDO menNeedInfo){
		return menNeedInfoDao.save(menNeedInfo);
	}
	
	@Override
	public int update(MenNeedInfoDO menNeedInfo){
		return menNeedInfoDao.update(menNeedInfo);
	}
	
	@Override
	public int remove(String id){
		return menNeedInfoDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return menNeedInfoDao.batchRemove(ids);
	}

    @Override
    public List<MenNeedInfoDO> likeList(Map<String, Object> map){
        return menNeedInfoDao.likeList(map);
    }

	@Override
	public List<Map<String, Object>> menNeedList(Map<String, Object> map){
		return menNeedInfoDao.menNeedList(map);
	}
    @Override
    public int likeListCount(Map<String, Object> map){
        return menNeedInfoDao.likeListCount(map);
    }

	@Override
	public R getNeedListByTypeId(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String,Object>> list  = menNeedInfoDao.getNeedListByTypeId(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);

	}

    @Override
    public R newNeedList(Map<String, Object> params) {
        //获取最新的需求
		PageHelperUtils.page(params);
		List<Map<String,Object>> list  = menNeedInfoDao.newNeedList(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);


    }

	@Override
	public R saveNeedInfo(Map<String, Object> params) {
		try {
			if(params.get("userId").toString().equals("-1")){
				return R.error("请登录");
			}else{
				MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
				if(memberUserDO==null){
					return R.error("用户信息有误,请重新登录");
				}
				MenNeedInfoDO menNeedInfoDO = new MenNeedInfoDO();
				BeanUtils.populate(menNeedInfoDO,params);
				menNeedInfoDO.setDelFlag(0);
				menNeedInfoDO.setUpAt(new Date());
				menNeedInfoDO.setCaAt(new Date());
				menNeedInfoDO.setId(UuidUtil.get32UUID());
				menNeedInfoDO.setRecommend(0);
				menNeedInfoDO.setStatus(memberUserDO.getLevel());
				menNeedInfoDao.save(menNeedInfoDO);
				return R.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}

    @Override
    public R getNeedListByTypeIdAndCity(Map<String, Object> params) {

		//获取最新的需求
		PageHelperUtils.page(params);
		List<Map<String,Object>> list  = menNeedInfoDao.getNeedListByTypeIdAndCity(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);

    }

	@Override
	public R getNeedInfo(Map<String, Object> params) {
		Map<String,Object> map = menNeedInfoDao.getNeedInfo(params);
		return R.ok().put("data",map);
	}

	@Override
	public R getRecommendNeed(Map<String, Object> params) {
		PageHelperUtils.page(params);
		params.put("recommend","1");
		List<Map<String,Object>> list  = menNeedInfoDao.getNeedListByTypeIdAndCity(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);
	}

	@Override
	public R getTodayNeed() {
		List<Map<String,Object>> list  = menNeedInfoDao.getTodayNeed();
		return R.ok().put("data",list);
	}

    @Override
    public R getNeedListAndType(Map<String, Object> params) {

		List<Map<String,Object>> list = menNeedTypeDao.getTypeList();
		if(list.size()>0){
			for (Map<String,Object> map : list) {

				params.put("typeId",map.get("typeId").toString());
				params.put("offset",0);
				params.put("limit",6);
				List<Map<String,Object>> needList  = menNeedInfoDao.getNeedListByTypeId(params);
				map.put("needList",needList);

			}
		}
		return R.ok().put("data",list);

	}


}
